<?php
/**
* @version 1.2.0
* @package RSSearch! 1.0.0
* @copyright (C) 2011 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/
defined( '_JEXEC' ) or die( 'Restricted access' ); 
class RssearchModelResults extends JModel{
	var $_total=null;
	
	function __construct(){
		$option = 'com_rssearch';	
		$mainframe = JFactory::getApplication();
		parent::__construct();
		
		
		$limit		= $mainframe->getUserStateFromRequest($option.'.results.limit', 'limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0, 'int');

		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	
	}

	function getResults(){
		header('Content-type: text/html; charset=utf-8');
	
		$db =& JFactory::getDBO();
		//$rsfilesConfig = RsfinderHelper::getConfig();
		$search = JRequest::getVar('search','');
		$module_id = JRequest::getInt('module_id');
		if(!$module_id) die();

		$search = $db->getEscaped($search);
		
		$db->setQuery("SELECT params FROM #__modules WHERE id = ".$module_id."");
		$params = $db->loadResult();


		jimport('joomla.html.parameter');
		jimport('joomla.plugin.helper');

		$params = new JParameter($params);

		$components = $params->get('comps');

		// Get plugins
		JPluginHelper::importPlugin('rssearch');
		$dispatcher =& JDispatcher::getInstance();

		$results = array();

		if(!empty($components)){
			if (!is_array($components)){
				$components = (array) $components;
			}
			foreach($components as $component){
				if ($plugin = JPluginHelper::getPlugin('rssearch',$component)){
	
					$className = 'plgRSSearch'.$plugin->name;
					if(class_exists($className)) {
						$instance = new $className($dispatcher, (array)$plugin);
						$instance->getResults($results, $search, $params);
					}
				}
			}
		}

		$this->_total = count($results);

		if($this->getState('limit') > 0){
			$results = array_slice($results, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $results;
	}

	function getTotal()
	{
		return $this->_total;
	}
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}
	function getWordsLimit()
	{
		$db =& JFactory::getDBO();
		$module_id = JRequest::getInt('module_id');
		if(!$module_id) die();
		$db->setQuery("SELECT params FROM #__modules WHERE id = ".$module_id."");
		$params = $db->loadResult();
		
		jimport('joomla.html.parameter');	
		
		$params = new JParameter($params);
		$nr_words = $params->get('nr_words');

		return $nr_words;
	}
}
?>